﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Model;
using Model.Interfaces;
using ViewModel.Interfaces;

namespace ViewModel
{
    public class SimpleProjectViewModel : ViewModelBase, ISimpleProjectViewModel
    {
        public SimpleProjectViewModel(IProject project, IViewModelObjectFactory factory)
        {
            _project = project;
            _project.ProjectChanged += ProjectProjectChanged;
            _factory = factory;
            _EditProjectCommand = _factory.CreateCommand(CanCreateEditProjectViewModel, CreateEditProjectViewModel);
        }

       

        private IProject _project;
        private IViewModelObjectFactory _factory;
        private ICommand _EditProjectCommand;
        private IEditProjectViewModel _ChildView;

        private void ProjectProjectChanged(object sender, string propertyName)
        {
            switch (propertyName)
            {
                case "Name":
                    NotifyPropertyChanged("ProjectName");
                    break;
                case "Description":
                    NotifyPropertyChanged("ShortProjectDescription");
                    break;
                case "Status":
                    NotifyPropertyChanged("ProjectStatus");
                    NotifyPropertyChanged("ProjectName");
                    NotifyPropertyChanged("ShortProjectDescription");
                    break;
            }
        }

        public string ProjectStatus
        {
            get 
            {
                if (_project.Status == Model.ProjectStatus.Active)
                    return _project.Status.ToString();
                else return "";
            }
        }

        public string ProjectName
        {
            get { return _project.Name; }
        }

        public string ShortProjectDescription
        {
            get 
            {
                if (_project.Status == Model.ProjectStatus.Active)
                    return _project.ShortDescription(50) + "...";
                else return "";
            }
        }

        public IEditProjectViewModel ChildView
        {
            get { return _ChildView; }
        }

        public ICommand EditProjectCommand
        {
            get { return _EditProjectCommand; }
        }

        public void CreateEditProjectViewModel()
        {
            _ChildView = _factory.CreateEditProjectViewModel(_project);
        }

        public bool CanCreateEditProjectViewModel()
        {
            if (_project.Status == Model.ProjectStatus.Active)
                return true;
            return false;
        }
    }
}